//
//  MOWaitDoDetail_IdeaViewController.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/6/7.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//  处理意见

#import "MOWaitDoDetail_IdeaViewController.h"
#import "MOWaitDoDetail_IdeaViewCell.h"

@interface MOWaitDoDetail_IdeaViewController ()<UITableViewDelegate,UITableViewDataSource,UITextViewDelegate>

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataArray;

@property(nonatomic,strong)UIView *headerView;

@property(nonatomic,strong)UIButton *rightButton;

//意见
@property(nonatomic,strong)SNTextView *textView;


@end

@implementation MOWaitDoDetail_IdeaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.tableView];
    
    if ([self.waitModel.comeStr isEqualToString:@"办理并注办"]||[self.waitModel.comeStr isEqualToString:@"部门传阅"]) {
        self.tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.001)];
    }else{
        if ([self.waitModel.wdlx isEqualToString:@"收文处理单"]) {//收文
            self.tableView.tableHeaderView = self.headerView;
        }else{
            self.tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.001)];
        }
    }
    
    if ([self.waitModel.wdlx isEqualToString:@"收文处理单"]) {//收文
        [self initData];
    }
    
}

-(void)setWaitIdeaArray:(NSMutableArray *)waitIdeaArray
{
    if ([self.waitModel.wdlx isEqualToString:@"收文处理单"]) {//收文
    }else{//发文
        
        self.dataArray = [NSMutableArray array];
        for (int i = 0; i<waitIdeaArray.count; i++) {
            
            NSDictionary *dataDic = waitIdeaArray[i];
            NSDictionary *dic = dataDic[dataDic.allKeys[0]];
            
            MOWaitDoDetail_IdeaModel *model = [[MOWaitDoDetail_IdeaModel alloc]init];
            
            model.resultName = dic[@"spr"];
            model.duty = dic[@"sprzw"];
            model.depyName = dic[@"spbmmc"];
            model.suggestion = dic[@"yj"];
            
            model.dateTime = dic[@"sprq"];
            
            model.processMode = dic[@"clfs"];
            
            model.declare = dic[@"spsm"];
            model.openStatus = @"0";
            
            [self.dataArray addObject:model];
            
        }
        [self.tableView reloadData];
    }
}

-(void)initData
{
    self.dataArray = [NSMutableArray array];
    
    if ([self.waitModel.comeStr isEqualToString:@"部门传阅"]) {
        
        NSString *url = [NSString stringWithFormat:@"%@/%@?authorization=%@",ODPSDB_ODPSGETBMCYCLYJ_URL,self.waitModel.key,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
        
        [PPNetworkHelper GET:url parameters:@{} success:^(id responseObject) {
            NSLog(@"收文处理单处理意见(部门传阅)：%@",responseObject);
            if (![responseObject[@"result"] isKindOfClass:[NSDictionary class]]) {
                return ;
            }
            NSDictionary *allDataDic = responseObject[@"result"];
            NSArray *arr = allDataDic.allKeys;
            for (int i = 0; i<arr.count; i++) {
                
                NSDictionary *dic = allDataDic[arr[i]];
//                NSDictionary *dic = dataDic[dataDic.allKeys[0]];
                
                MOWaitDoDetail_IdeaModel *model = [[MOWaitDoDetail_IdeaModel alloc]init];
                model.resultName = dic[@"spr"];
                model.duty = dic[@"sprzw"];
                model.depyName = dic[@"spbmmc"];
                model.suggestion = dic[@"yj"];
                
                model.dateTime = dic[@"sprq"];
                
                model.processMode = dic[@"clfs"];
                
                model.declare = dic[@"spsm"];
                model.openStatus = @"0";
                
                [self.dataArray addObject:model];
                
            }
            [self.tableView reloadData];
            //        }
            
        } failure:^(NSError *error) {
            NSLog(@"收文处理单处理意见(部门传阅)：%@",[error localizedDescription]);
        }];
        
    }else{
        NSString *url = [NSString stringWithFormat:@"%@/%@?authorization=%@",ODPSDB_ODPSQUERYSWCLDCLYJ_URL,self.waitModel.key,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
        
        [PPNetworkHelper GET:url parameters:@{} success:^(id responseObject) {
            NSLog(@"收文处理单处理意见：%@",responseObject);
            //        if ([responseObject[@"status"] integerValue]==1) {
            if (![responseObject[@"result"] isKindOfClass:[NSArray class]]) {
                return ;
            }
            NSArray *arr = responseObject[@"result"];
            for (int i = 0; i<arr.count; i++) {
                
                NSDictionary *dataDic = arr[i];
                NSDictionary *dic = dataDic[dataDic.allKeys[0]];
                
                MOWaitDoDetail_IdeaModel *model = [[MOWaitDoDetail_IdeaModel alloc]init];
                model.resultName = dic[@"spr"];
                model.duty = dic[@"sprzw"];
                model.depyName = dic[@"spbmmc"];
                model.suggestion = dic[@"yj"];
                
                model.dateTime = dic[@"sprq"];
                
                model.processMode = dic[@"clfs"];
                
                model.declare = dic[@"spsm"];
                model.openStatus = @"0";
                
                [self.dataArray addObject:model];
                
            }
            [self.tableView reloadData];
            //        }
            
        } failure:^(NSError *error) {
            NSLog(@"收文处理单处理意见：%@",[error localizedDescription]);
        }];
    }
    
}

-(void)clickOpenButton:(UIButton *)sender
{
    MOWaitDoDetail_IdeaModel *model = self.dataArray[sender.tag];
    if ([model.openStatus integerValue]==1) {
        model.openStatus = @"0";
    }else{
        model.openStatus = @"1";
    }
    [self.tableView reloadData];
}

-(void)clickRightButton
{
    SNXYSelectedNumberView *pickview = [[SNXYSelectedNumberView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    //已阅  同意  拟同意  不同意
    NSArray *arr = @[@"已阅",@"同意",@"拟同意",@"不同意"];
    pickview.numberArray = arr;
    
    __weak typeof (self)weakSelf = self;
    pickview.clickIndexBlock = ^(NSInteger index) {
        [weakSelf.rightButton setTitle:arr[index] forState:UIControlStateNormal];
        [weakSelf.rightButton SG_imagePositionStyle:(SGImagePositionStyleRight) spacing:10];
    
        [[NSNotificationCenter defaultCenter]postNotificationName:@"waitDoDetail_Idea_MyIdea" object:nil userInfo:@{@"text":arr[index]}];

    };
    [WINDOW addSubview:pickview];
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MOWaitDoDetail_IdeaModel *model = self.dataArray[indexPath.row];
    
    MOWaitDoDetail_IdeaViewCell *cell = [MOWaitDoDetail_IdeaViewCell cellWithTableView:tableView andIndexPath:indexPath];
    cell.model = model;
    
    cell.rightButton.tag = indexPath.row;
    [cell.rightButton addTarget:self action:@selector(clickOpenButton:) forControlEvents:UIControlEventTouchUpInside];
    
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MOWaitDoDetail_IdeaModel *model = self.dataArray[indexPath.row];
    return [MOWaitDoDetail_IdeaViewCell heightForMOWaitDoDetail_IdeaViewCellAndModel:model];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.00001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}


#pragma mark - 设置顶头分割线
-(void)viewDidLayoutSubviews {
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])  {
        if (@available(iOS 8.0, *)) {
            [self.tableView setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
        } else {
            // Fallback on earlier versions
        }
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        if (@available(iOS 8.0, *)) {
            [cell setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
        } else {
            // Fallback on earlier versions
        }
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
}


-(void)textViewDidEndEditing:(UITextView *)textView
{
    //审批说明
    [[NSNotificationCenter defaultCenter]postNotificationName:@"waitDoDetail_Idea_desc" object:nil userInfo:@{@"text":textView.text}];
}

#pragma mark -- 懒加载
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - StatusBarAndNavigationBarHeight - 50 - 41 - TabbarSafeBottomMargin) style:(UITableViewStyleGrouped)];
        _tableView.backgroundColor = KGrayColor_ededed;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

-(UIView *)headerView
{
    if (!_headerView) {
        _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kHConversions(191))];
        _headerView.backgroundColor = KGrayColor_ededed;
        
        UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH, kHConversions(191)-10)];
        backView.backgroundColor = [UIColor whiteColor];
        [_headerView addSubview:backView];
        
        UIButton *nameButton = [[UIButton alloc]initWithFrame:CGRectMake(17, 0, 100, kHConversions(54))];
        [nameButton setTitleColor:KBlackColor_2A2A2A forState:UIControlStateNormal];
        nameButton.titleLabel.font = FONT_SIZE(14);
        [nameButton setTitle:@"我的意见:" forState:UIControlStateNormal];
        [nameButton setContentHorizontalAlignment:(UIControlContentHorizontalAlignmentLeft)];
        [backView addSubview:nameButton];
        
        self.rightButton = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-90, 0, 100, kHConversions(54))];
        self.rightButton.titleLabel.font = FONT_SIZE(15);
        [self.rightButton setTitle:@"请选择" forState:UIControlStateNormal];
        [self.rightButton setImage:IMAGE(@"right_notice") forState:UIControlStateNormal];
        [self.rightButton SG_imagePositionStyle:(SGImagePositionStyleRight) spacing:10];
        [self.rightButton setTitleColor:KGrayColor_BABEC2 forState:UIControlStateNormal];
        [self.rightButton addTarget:self action:@selector(clickRightButton) forControlEvents:UIControlEventTouchUpInside];
        [backView addSubview:self.rightButton];
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, self.rightButton.bottom, SCREEN_WIDTH, 1)];
        lineView.backgroundColor = KGrayColor_ededed;
        [backView addSubview:lineView];
        
        UIButton *descLabel = [[UIButton alloc]initWithFrame:CGRectMake(17, lineView.bottom, 100, kHConversions(54))];
        [descLabel setTitleColor:KBlackColor_2A2A2A forState:UIControlStateNormal];
        descLabel.titleLabel.font = FONT_SIZE(14);
        [descLabel setTitle:@"审批说明:" forState:UIControlStateNormal];
        [descLabel setContentHorizontalAlignment:(UIControlContentHorizontalAlignmentLeft)];
        [backView addSubview:descLabel];
        
        self.textView = [[SNTextView alloc]initWithFrame:CGRectMake(kWConversions(13), descLabel.bottom,(int)(SCREEN_WIDTH - kWConversions(13)*2), (int)(backView.height - 1 - kHConversions(54)*2))];
        self.textView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
        self.textView.backgroundColor = [UIColor whiteColor];
        self.textView.placeholder = @"请输入审批内容";
        self.textView.placeholderColor = KGrayColor_BABEC2;
        self.textView.font = FONT_SIZE(12);
        self.textView.delegate = self;
        self.textView.textColor = KBlackColor_2A2A2A;
        [backView addSubview:self.textView];
        
    }
    return _headerView;
}







@end
